#include "stdafx.h"
#include <http_listener.h>

using namespace http;
using namespace http::listener;

// Declare a global http_listener
http_listener g_listener;

// This function will be called by Casablanca when HelloWorld.dll is loaded.
// Often this will include things like creating an http_listener or hosting/spawning actors.
// 'args' contains any command line arguments, which can only be specified when running locally not in Azure.
// 'configs' contains any settings from actors_config.cfg and the port numbers for each Azure endpoint. 
bool on_initialize(const std::vector<casablanca::string_t> &args, const std::vector<casablanca::string_t> &configs)
{
	// Create the http_listener
    g_listener = http_listener::create(U("http://localhost:8181"));

    // Provide the listener with a lambda that gets called when the HTTP GET method is received
	// The lambda does not close over any instance or local variables, so is effectively a free-standing static method
    g_listener.support(methods::GET, [](http_request message)
    {
        message.reply(http::status_codes::OK, U("Hello World!"));
        actors::log::post(actors::LOG_INFO, U("Serviced a GET request for ") + message.request_uri().to_string());
    });

	// support other HTTP verbs specified by the methods:: enumeration

	// open the connection and return a value indicating successful opening
    return g_listener.open() == 0;
}

// This will be called by Casablanca when HelloWorld.dll is unloaded.
bool on_shutdown()
{
	// close the connection and return a value indicating successful closing
    return g_listener.close() == 0;
}